package opdracht4;

import java.util.ArrayList;

public class Graph {
	public static final int RED = 0;
	public static final int GREEN = 1;
	public static final int WHITE = 2;
	public static final int BLUE = 3;

	ArrayList<Node> nodes;

	public Graph() {
		nodes = new ArrayList<Node>();
	}

	public static String getColor(int color) {
		switch (color) {
		case RED:
			return "red";
		case GREEN:
			return "green";
		case WHITE:
			return "white";
		case BLUE:
			return "blue";
		default:
			return "no color";
		}
	}

	public void addNode(int color) {
		nodes.add(new Node(color));
	}

	public void initialize() {
		for (int i = 0; i < 4; i++) {
			addNode(i);
		}
		connect(BLUE, GREEN, 1);
		connect(BLUE, WHITE, 1);
		connect(GREEN, RED, 1);

		connect(BLUE, RED, 2);
		connect(GREEN, GREEN, 2);
		connect(RED, WHITE, 2);

		connect(BLUE, GREEN, 3);
		connect(BLUE, RED, 3);
		connect(RED, WHITE, 3);

		connect(BLUE, RED, 4);
		connect(GREEN, RED, 4);
		connect(GREEN, WHITE, 4);
	}

	public void connect(int c1, int c2, int weight) {
		nodes.get(c1).addConnection(nodes.get(c2), weight);
	}

	public class Node {

		int color;
		ArrayList<Connection> connections;

		public Node(int color) {
			this.color = color;
			connections = new ArrayList<Connection>();
		}

		public void addConnection(Node node, int weight) {
			Connection connection = new Connection(this, node, weight);
			connections.add(connection);
			node.addConnection(connection);
		}

		public void addConnection(Connection connection) {
			if (connection != null)
				connections.add(connection);
		}

		public String toString() {
			String string = "Node color: " + getColor(color) + ", connections:";
			for (Connection connection : connections) {
				string += "\n" + getColor(connection.node1.color) + "," + getColor(connection.node2.color) + "(" + connection.weight + ")";
			}
			return string + "\n";
		}
	}

	public class Connection {

		Node node1;
		Node node2;
		int weight;

		public Connection(Node node1, Node node2, int weight) {
			this.node1 = node1;
			this.node2 = node2;
			this.weight = weight;
		}
		
		public String toString() {
			return getColor(node1.color) + "<--" + weight + "-->" + getColor(node2.color);
		}
	}

	public String toString() {
		String string = "";
		for (Node node : nodes) {
			string += node.toString();
		}
		return string;
	}

}
